function createThead(){
	var appendText = "";
	
	appendText += needFunction.checkFun ? "<td width='"+needFunction.checkFun+"'><input type='checkbox' id='checkboxAll' /></td>" : "" ;
	for (var ikey in tableTitle){ 
		appendText += "";
		appendText += "<th width='"+tableTitle[ikey][2]+"'>";
		appendText += "<img onclick='searchSort(\""+tableTitle[ikey][1]+" desc\")' class='tableOrderImg' src='../images/icons/sortdown.png'></img>";
		appendText += tableTitle[ikey][0];
		appendText += "<img  onclick='searchSort(\""+tableTitle[ikey][1]+" asc\")'  class='tableOrderImg' src='../images/icons/sortup.png'></img>";
		appendText += "</th>";
	}
	appendText += needFunction.lastFun ?  "<th width='"+needFunction.lastFunWitdh+"'>"+needFunction.lastFunName+"</th>" : "" ;
	
	$("#"+tbodyArray.theadId).html(appendText);
}

function createTfoot(){
	
	var colspanSize = tableTitle.length;
	colspanSize += needFunction.checkFun ? 1 :0 ;
	colspanSize += needFunction.lastFun ? 1 :0 ;
	
	var appendText = "";
	appendText += "<tr><td colspan='"+ colspanSize +"'>";
	appendText += "	<div style='float:left; margin-top:15px;' id='countResult' ></div>";
	appendText += "	<div class='pagination' id='serachResultPagesDesc'>	</div>";
	appendText += "	<div class='pagination' id='serachResultPages'> 	</div>";
	appendText += "	<div class='pagination' id='serachResultPagesAsc' >	</div>";
	appendText += "</td></tr>";
	
	$("#"+tbodyArray.tfootId).html(appendText);
}

function createTbody(){
	
	$.ajax({
		url : config.functionPath + className + "S.php?action=" + tbodyArray['functionName'],
		dataType: "json" , 
		type: 'POST',
		cache : false,
		data : {datas : {
			orderData : tableDataArr['orderData'],
			pageRow :  tableDataArr['pageRow'],
			page : tableDataArr['page'],
			searchData : searchData
		}},
		beforeSend : function (){ showWait('資料取得中...'); } 
	}).done(function(result){
		hideWait();
		if(!checkAjaxResult(result)){return;}
		if(result['data']['result']['count'] == 0){alert("查無資料!"); return;}
		
		var showData = "";
		for (var i in result['data']['result']){
			showData += "<tr>";
			showData += needFunction.checkFun ? "<td><input type='checkbox' name='checkboxGroup' value='"+result['data']['result'][i][needFunction.checkFunKey]+"' /></td>" : "" ;
			
			for(var j in dataArray){
				showData += needFunction.needDetail ? "<td><a href='javascript:toEdit(\""+result['data']['result'][i]['id']+"\");' >"+result['data']['result'][i][dataArray[j]]+"</a></td>" : "<td>"+result['data']['result'][i][dataArray[j]]+"</td>" ;
			}
			if(needFunction.lastFun){
				showData += "<td>";
				for(var j in tabFunArray){
					if(tabFunArray[j]["checkDB"]){
						if( result['data']['result'][i][tabFunArray[j]["resultKey"]] == 1 ){
							showData += "<img class='tableFunImg' "+(tabFunArray[j]["showCursor"]?"style='cursor:pointer;'":"")+" align='center' onclick='"+tabFunArray[j]["functionTrue"]+"("+result['data']['result'][i][tabFunArray[j]["dataKey"]]+")' title='"+tabFunArray[j]["showTrue"]+"' src='../images/icons/"+tabFunArray[j]["imgTrue"]+"' />&nbsp;";
						}else{
							showData += "<img class='tableFunImg' "+(tabFunArray[j]["showCursor"]?"style='cursor:pointer;'":"")+" align='center' onclick='"+tabFunArray[j]["functionFalse"]+"("+result['data']['result'][i][tabFunArray[j]["dataKey"]]+")' title='"+tabFunArray[j]["showFalse"]+"' src='../images/icons/"+tabFunArray[j]["imgFalse"]+"' />&nbsp;";
						}
					}else{
						showData += "<img class='tableFunImg' "+(tabFunArray[j]["showCursor"]?"style='cursor:pointer;'":"")+" align='center' onclick='"+tabFunArray[j]["functionTrue"]+"("+result['data']['result'][i][tabFunArray[j]["dataKey"]]+")' title='"+tabFunArray[j]["showTrue"]+"' src='../images/icons/"+tabFunArray[j]["imgTrue"]+"' />&nbsp;";
					}
				}
				showData += "</td>";
			}
			showData += "</tr>";
		}
		$("#"+tbodyArray['tbodyId']).html(showData);
		makeTfoot(result['data']['count'], result['data']['result']);
		
	}).fail(function (error){	
			hideWait(); alert(error.responseText); 	
	}); 
	
	if(needFunction.checkFun){
		$("#checkboxAll").click(function(){
			var isChecked = document.getElementById("checkboxAll").checked;
			if(isChecked){
				$("input[name='checkboxGroup']").each(function() {this.checked = true;});
			}else{
				$("input[name='checkboxGroup']").each(function() {this.checked = false;});          
			}
		});
	}
}

function makeTfoot(count, result){
	//取得總頁數
	var pageCount = count;
	var pages = Math.ceil(pageCount / tableDataArr['pageRow']);
	
	$("#countResult").html("");
	$("#countResult").html("資料筆數:&nbsp;"+count+"&nbsp;筆");
	
	//新增頁數連結
	var appendText = "";
	
	$("#serachResultPagesAsc").html("");
	$("#serachResultPagesDesc").html("");
	$("#serachResultPages").html("");
	
	$("#serachResultPagesAsc").html(
		'<a href="javascript:void(0);" onclick="serachByConditions(1)" title="First Page" id="first" > 第一頁</a>' + 
		'<a href="javascript:void(0);" onclick="serachByConditions(' + ( tableDataArr['page'] <=1 ? 1 : ( tableDataArr['page'] - 1 )) + ')" title="Previous Page" id="previous" > 上一頁</a>'
	);
	
	$("#serachResultPagesDesc").html(
		'<a href="javascript:void(0);" onclick="serachByConditions(' + ( tableDataArr['page'] >=  pages ? pages :tableDataArr['page'] + 1 ) + ')" title="Next Page" id="next" >下一頁 </a>' +
		'<a href="javascript:void(0);" onclick="serachByConditions(' + pages + ')" title="Last Page" id="last" >最後一頁</a>'
	);
	
	for (var i = 1 ; i <= pages ; i++) {
		appendText += '<a href="javascript:void(0);" onclick="serachByConditions(' + i + ')" class="' + ( i == tableDataArr['page'] ? 'number current' : 'number' ) + '" title="' + i + '">' + i + '</a>';
	}
	
	$("#serachResultPages").html(appendText);
}

function searchSort(str){
	tableDataArr['orderData'] = str;
	createTbody();
}

function serachByConditions(page){
	tableDataArr['page'] = page;
	createTbody();
}

